package com.hxl.tech.gateway.auth.controller;

import com.hxl.tech.gateway.auth.annotate.ApiPrivilege;
import com.hxl.tech.gateway.auth.dto.Response;
import com.hxl.tech.gateway.auth.dto.resource.ResourceDeleteDTO;;
import com.hxl.tech.gateway.auth.dto.resource.ResourceFindDTO;
import com.hxl.tech.gateway.auth.dto.resource.ResourceSaveDTO;
import com.hxl.tech.gateway.auth.dto.resource.ResourceDTO;
import com.hxl.tech.gateway.auth.model.Resource;
import com.hxl.tech.gateway.auth.service.ResourceService;
import com.hxl.tech.gateway.auth.util.CopyUtil;
import com.hxl.tech.gateway.common.constant.AppConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 资源配置 Controller 
 * @author soliddq
 * @date 2023-10-10
 */
@Api(value = "资源配置", tags = "资源配置")
@RestController
@RequiredArgsConstructor
public class ResourceController {

    private final ResourceService resourceService;

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.resource.list")
    @ApiOperation(value = "查询资源配置", response = ResourceDTO.class)
    public Response list(@RequestBody @Validated ResourceFindDTO findDTO) {

        return Response.createSuccess(CopyUtil.toList(resourceService.find(findDTO), ResourceDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.resource.save")
    @ApiOperation(value = "新增或更新资源配置", response = ResourceDTO.class)
    public Response saveOrUpdate(@RequestBody @Validated ResourceSaveDTO saveDTO) {

        return Response.createSuccess(CopyUtil.to(resourceService.saveOrUpdate(CopyUtil.to(saveDTO, Resource.class)), ResourceDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.resource.delete")
    @ApiOperation(value = "删除资源配置", response = ResourceDTO.class)
    public Response delete(@RequestBody @Validated ResourceDeleteDTO deleteDTO) {

        resourceService.delete(new Resource.Id(deleteDTO.getMerchantCode(), deleteDTO.getResourceType()));
        return Response.createSuccess();
    }

}
